package a66weding.com.jiehuntong.activity.order;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import org.xutils.x;

import java.io.Serializable;
import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.me.MeBaseActivity;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.MyOrderBean;
import a66weding.com.jiehuntong.common.CommonAdapter;
import a66weding.com.jiehuntong.common.MyViewHolder;

/**
 * 退换货第一个界面
 */
public class ApplyServicePreFirstActivity extends MeBaseActivity implements View.OnClickListener {

    private LinearLayout mBackLay;
    private TextView mBackTv;
    private TextView mHeadeTitle;
    private TextView mRightFunc;

    private ListView mListview;
    private TextView mOrderNumTv;
    private TextView mOrderTimeTv;
    private MyOrderBean.DataBean orderDataBean;
    private CommonAdapter mAdapter;
    private List<MyOrderBean.DataBean.MyOrderDetailBean> myOrderDetailList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply_service_pre_first);
        ((GlobalApplication) getApplication()).getActivityManager().addActivity("ApplyServiceOneActivity", this);
        tuihuanActivityMap.put("ApplyServiceOneActivity",this);




        Intent intent = getIntent();
        if(intent!=null) {
            orderDataBean = (MyOrderBean.DataBean) intent.getSerializableExtra("orderDataBean");
            myOrderDetailList = orderDataBean.getMyOrderDetail();
        }
        initview();
    }

    private void initview() {

        mBackLay = (LinearLayout) findViewById(R.id.llay_header_back);
        mBackTv = (TextView) findViewById(R.id.tv_header_backcontent);
        mHeadeTitle = (TextView) findViewById(R.id.tv_header_title);
        mRightFunc = (TextView) findViewById(R.id.tv_header_function);
        mOrderNumTv = (TextView) findViewById(R.id.tv_orderNum);
        mOrderTimeTv = (TextView) findViewById(R.id.tv_order_buytime);


        mListview = (ListView) findViewById(R.id.lview_service);
        mBackTv.setText("订单详情");
        mHeadeTitle.setText("售后服务");

        mRightFunc.setVisibility(View.GONE);

        if(orderDataBean!=null) {

            mOrderNumTv.setText(orderDataBean.getOrdersNumber());
            String orderSubmitDatetime = orderDataBean.getOrderSubmitDatetime();
            if(orderSubmitDatetime.contains("T")) {
                String[] ts = orderSubmitDatetime.split("T");

                mOrderTimeTv.setText(ts[0] +" " + ts[ts.length-1]);
            }

            mAdapter = new CommonAdapter<MyOrderBean.DataBean.MyOrderDetailBean>(ApplyServicePreFirstActivity.this,myOrderDetailList,R.layout.item_orderdetail_service) {

                @Override
                public void convert(MyViewHolder holder, int position) {
                    final MyOrderBean.DataBean.MyOrderDetailBean myOrderDetailBean = myOrderDetailList.get(position);

                    holder.setText(R.id.tv_indent_code,myOrderDetailBean.getProductName());
                    holder.setText(R.id.tv_me_classify,"x"+myOrderDetailBean.getBuyNumber());
                    ImageView logoIv = (ImageView) holder.getView(R.id.img_indent);
                    TextView serviceTv = (TextView) holder.getView(R.id.tv_toFuwu);
                    x.image().bind(logoIv,myOrderDetailBean.getProductPicMain());

                    serviceTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(ApplyServicePreFirstActivity.this, ApplyServiceOneActivity.class);
                            intent.putExtra("orderDataBean",myOrderDetailBean);
                            startActivity(intent);
                        }
                    });

                    boolean b_return = myOrderDetailBean.isB_Return();
                    boolean b_exchange = myOrderDetailBean.isB_Exchange();
                    boolean b_complaint = myOrderDetailBean.isB_Complaint();
                    int buyNumber = myOrderDetailBean.getBuyNumber();
                    if(myOrderDetailBean.getOrderReturn()!=null) {
                        int i_number = myOrderDetailBean.getOrderReturn().getI_Number();
                        if(b_return || b_exchange || b_complaint) {
                            if(i_number<buyNumber) {
                                serviceTv.setVisibility(View.VISIBLE);
                            }else{
                                serviceTv.setVisibility(View.GONE);
                            }
                        }else{
                            serviceTv.setVisibility(View.VISIBLE);

                        }

                    }else{

                        if(b_return || b_exchange || b_complaint) {
                                serviceTv.setVisibility(View.GONE);
                        }else{
                            serviceTv.setVisibility(View.VISIBLE);

                        }

                    }

                }
            };

            mListview.setAdapter(mAdapter);


        }

        mBackLay.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
                case R.id.llay_header_back:

                    finish();

                break;

        }
    }
}
